package com.boot.project.service.impl;

import com.boot.project.Constance;
import com.boot.project.dao.InfoDao;
import com.boot.project.domain.InfoDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.project.service.InfoService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InfoServiceImpl implements InfoService {
    @Autowired
    private InfoDao infoDao;

    @Override
    public List<InfoDo> rotation(Integer infoType) {
        return infoDao.rotation(infoType);
    }

    @Override
    public List<InfoDo> list(Integer pageNum, Integer pageSize, Integer infoType) {
        // 分页查询数据库的起始位位置
        Integer startIndex = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap();
        map.put("startIndex", startIndex);
        map.put("pageSize", pageSize);
        map.put("infoType", infoType);
        return infoDao.list(map);
    }

    @Override
    public Integer count(Integer infoType) {
        return infoDao.count(infoType);
    }

    @Override
    public InfoDo getInfo(Long id) {
        return infoDao.getInfo(id);
    }

    @Override
    public String upload(MultipartFile file, Long infoType) throws IOException {
        // 图片存储路径
        String fileSavePath = Constance.uploadPath + File.separator + infoType + File.separator +
                file.getOriginalFilename();
        File file1 = new File(fileSavePath);
        // 如果文件已经存在，说明之前上传过同名图片，需要将这次的文件名改掉
        if (file1.exists()) {
            String rangeCode = Constance.rangeCode();
            fileSavePath = Constance.uploadPath + File.separator + infoType + File.separator +
                   rangeCode + file.getOriginalFilename();
        }
        // 如果父级文件夹不存在则创建文件夹
        if (!file1.getParentFile().exists()) {
            file1.getParentFile().mkdirs();
        }
        File file2 = new File(fileSavePath);
        file.transferTo(file2);
        return fileSavePath;
    }

    @Override
    public Map<String, Object> saveInfo(InfoDo infoDo) {
        Map<String, Object> mapResult = new HashMap<>();
        infoDo.setCreatedTime(new Date());
        infoDo.setComments(0L);
        Integer saveFlag = infoDao.saveInfo(infoDo);
        if (saveFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> deleteInfo(Long id) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer deleteFlag = infoDao.deleteInfo(id);
        if (deleteFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }
}
